package com.zshan.clinic.admin.service.customer;

import com.zshan.clinic.admin.entity.vo.ClinicDeptSelectVo;
import com.zshan.clinic.admin.entity.vo.ClinicRegisterVo;
import com.zshan.clinic.admin.entity.vo.DeptRemainVo;
import com.zshan.clinic.admin.entity.vo.DoctorRemainVo;
import com.zshan.clinic.admin.service.clinic.ClinicDeptService;
import com.zshan.clinic.admin.service.medical.ClinicRegisterService;
import com.zshan.clinic.common.enums.RegChannelEnum;
import com.zshan.clinic.common.enums.RegMethodEnum;
import com.zshan.clinic.database.model.ClinicRegister;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * 预约管理
 */
@Service
@Slf4j
public class AppointmentService {

    @Autowired
    private ClinicRegisterService clinicRegisterService;

    @Autowired
    private ClinicDeptService clinicDeptService;

    /**
     * 查询部门类表
     * @param clinicId
     * @return
     */
    public List<ClinicDeptSelectVo> getDeptList(Long clinicId) {
        return clinicDeptService.getSelectList(clinicId);
    }

    /**
     * 查询预约列表
     * @param clinicId
     * @param cmUserId
     * @param regStatus
     * @param pageNo
     * @param pageSize
     * @return
     */
    public List<ClinicRegisterVo> getAppointmentList(Long clinicId, Long cmUserId, String regStatus, Integer pageNo, Integer pageSize) {
        return clinicRegisterService.getAppointmentList(clinicId,cmUserId,regStatus,pageNo,pageSize);
    }

    /**
     * 取消预约
     * @param clinicRegister
     */
    @Transactional(rollbackFor = Exception.class)
    public void cancelAppointment(ClinicRegister clinicRegister) {
        clinicRegisterService.cancelRegister(clinicRegister);
    }

    /**
     * 预约挂号
     * @param clinicRegister
     */
    @Transactional(rollbackFor = Exception.class)
    public void addAppointment(ClinicRegister clinicRegister) {
        clinicRegister.setRegMethod(RegMethodEnum.REGISTER_APPOINTMENT.getValue());
        clinicRegister.setRegChannel(RegChannelEnum.WECHAT_MINI.getValue());
        clinicRegisterService.addRegister(clinicRegister);
    }

    /**
     * 查询医生余号列表
     * @param clinicId
     * @param deptId
     * @return
     */
    public List<DoctorRemainVo> getRemainList(Long clinicId, Long deptId, LocalDate visitDate) {
        List<DeptRemainVo> deptRemainVoList = clinicRegisterService.getRemainList(clinicId,deptId,visitDate,null,null);
        if(CollectionUtils.isEmpty(deptRemainVoList)){
            return Collections.EMPTY_LIST;
        }
        return deptRemainVoList.get(0).getRemainList();
    }
}
